package com.umeox.watch.moto.chat.ui

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.RemoteException
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity
import com.umeox.moto.watch.themes.widget.TextView
import com.umeox.watch.moto.chat.R
import com.umeox.watch.moto.chat.adapter.DeleteFriendSelectListAdapter
import com.umeox.watch.moto.chat.bean.DeleteGroupMemberInfo
import com.umeox.watch.moto.chat.bean.GroupMemberDetail
import com.umeox.watch.moto.dataservice.DataProvider
import com.umeox.watch.moto.dataservice.proxy.PushApiClientProxy
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class DeleteGroupFriendActivity : ThemeAppCompatActivity(),
    DeleteFriendSelectListAdapter.OnItemClickListener {

    companion object {
        fun intoDeleteGroupFriendActivity(context: Context, groupId: String) {
            context.startActivity(Intent(context, DeleteGroupFriendActivity::class.java).apply {
                putExtra("groupId", groupId)
            })
        }
    }

    private lateinit var ivDelete: TextView
    private lateinit var contactList: RecyclerView
    private val adapter = DeleteFriendSelectListAdapter()
    private var isCreating = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_delete_group_member)

        val groupId = intent?.getStringExtra("groupId")!!

        ivDelete = findViewById(R.id.iv_delete)
        contactList = findViewById(R.id.contactList)
        contactList.adapter = adapter
        adapter.setOnItemClickListener(this)
        ivDelete.setOnClickListener {
            if (isCreating) {
                return@setOnClickListener
            }
            isCreating = true
            deleteGroupFriends(groupId)
        }
        getData(groupId)
    }

    private fun deleteGroupFriends(groupId: String) {
        lifecycleScope.launch(Dispatchers.IO) {
            try {
                val friendIds = adapter.getSelectedFriendIds()
                deleteGroupFriends(groupId, friendIds)
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                finish()
            }
        }
    }

    private fun getData(groupId: String) {
        lifecycleScope.launch(Dispatchers.IO) {
            val list = getDeviceFriendsList(groupId)
            withContext(Dispatchers.Main) {
                adapter.setData(list)
            }
        }
    }

    private fun getDeviceFriendsList(groupId: String): List<DeleteGroupMemberInfo> {
        //获取群友列表
        val groupMemberList = getGroupMemberList(groupId)

        val deleteGroupMemberInfoList = mutableListOf<DeleteGroupMemberInfo>()
        //生成显示列表
        for (member in groupMemberList) {
            val deviceFriendInfo = DeleteGroupMemberInfo(member, available = true, selected = false)
            //剔除自己
            if (DataProvider.getHolderId() != member.memberId) {
                deleteGroupMemberInfoList.add(deviceFriendInfo)
            }
        }

        return deleteGroupMemberInfoList
    }

    private fun getGroupMemberList(groupId: String): List<GroupMemberDetail> {
        val list = mutableListOf<GroupMemberDetail>()
        val memberList = DataProvider.getIMGroupMembersByGroupId(groupId)
        if (!memberList.isNullOrEmpty()) {
            for (temp in memberList) {
                val task: GroupMemberDetail? = GroupMemberDetail.convertFromValues(temp)
                if (task != null) {
                    list.add(task)
                }
            }
        }
        return list
    }

    @Throws(RemoteException::class)
    fun deleteGroupFriends(groupId: String, friendIds: List<String>) {
        PushApiClientProxy.fetchBinder().removeGroupMember(groupId, friendIds)
    }


    @SuppressLint("NotifyDataSetChanged")
    override fun onItemClick(position: Int, data: DeleteGroupMemberInfo) {
        data.selected = !data.selected
        adapter.notifyItemChanged(position)
    }
}